package httpserver

import (
	"github.com/gin-gonic/gin"
)

//RouteFromGin 从gin进行路由配置
func (s *HttpServer) RouteFromGin(r *gin.Engine) *HttpServer {
	for _, v := range r.Routes() {
		s.addRouteHandler(v.Method, v.Path, r, nil)
	}
	return s
}

//RouteToGin 路由配置copy至gin
func (s *HttpServer) RouteToGin(engine *gin.Engine) *HttpServer {
	routers := map[string]*Router{}
	for _, x := range s.routers {
		routers[x.hash()] = x
	}

	dict := map[string]bool{}
	for _, x := range engine.Routes() {
		dict[x.Method+"_"+x.Path] = true
	}

	for _, r := range routers {
		if dict[r.Method+"_"+r.Path] {
			continue
		}
		engine.Handle(r.Method, r.Path, r.handlerGin(s.config))
	}

	return s
}
